package chances.system.operator.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import chances.system.operator.entity.Menu;
import chances.system.operator.entity.Operator;
import chances.system.operator.entity.Organization;
import chances.system.operator.entity.Permission;
import chances.system.operator.entity.Role;
import chances.webui.model.Tree;

public class OperatorSession implements UserDetails{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final static String SESSION_NAME="CSCMS_USERSESSION";
	
	private Map<String,String> pageStatus = new HashMap<String,String>();

	//用户
	private Operator operator;
	
	//可以访问的组织
	private List<Organization> accessOrgs;

	//菜单类型的权限列表
	List<Permission> permissions;
	
	//操作类型的权限列表

	
	//转好的菜单
	Tree<Menu> menuTree;
	
	//构造器
	public OperatorSession(){
		super();
	}
	
	public OperatorSession(Operator user, Tree<Menu> tree) {
		super();
		this.operator = user;
		this.menuTree = tree;
	}
	
	//static方法
	public static OperatorSession getOperatorSession(HttpServletRequest request){
		return (OperatorSession)request.getSession().getAttribute(SESSION_NAME);
	}
	
	public static void saveOperatorSession(HttpServletRequest request,OperatorSession us){
		request.getSession().setAttribute(SESSION_NAME, us);
	}
	
	public static void removeOperatorSession(HttpServletRequest request){
		request.getSession().removeAttribute(SESSION_NAME);
	}
	
	/***getter setter***/
	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator user) {
		this.operator = user;
	}

	public List<Organization> getAccessOrgs() {
		return accessOrgs;
	}

	public void setAccessOrgs(List<Organization> accessOrgs) {
		this.accessOrgs = accessOrgs;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public Tree<Menu> getMenuTree() {
		return menuTree;
	}

	public void setMenuTree(Tree<Menu> menuTree) {
		this.menuTree = menuTree;
	}

	public Map<String, String> getPageStatus() {
		return pageStatus;
	}

	public void setPageStatus(Map<String, String> pageStatus) {
		this.pageStatus = pageStatus;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();  
        GrantedAuthority au = new SimpleGrantedAuthority("ROLE_USER");  
        list.add(au);
        
        for (Role role : this.operator.getRoles()) {
        	GrantedAuthority authority = new SimpleGrantedAuthority(role.getName());  
            list.add(authority);  
		}
        
        return list; 
	}

	@Override
	public String getPassword() {
		return this.operator.getPassword();
	}

	@Override
	public String getUsername() {
		return this.operator.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return this.operator.isAccountNonExpired();
	}

	@Override
	public boolean isAccountNonLocked() {
		return this.operator.isAccountNonLocked();
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return this.operator.isCredentialsNonExpired();
	}

	@Override
	public boolean isEnabled() {
		return  this.operator.isEnabled();
	}

	public String getSalt() {
		return operator.getSalt();
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof OperatorSession) {  
	        return operator.getUsername().equals(((OperatorSession)obj).getUsername());  
	    }  
	    return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return operator.getUsername().hashCode();
	}

	public Long getId() {
		return operator.getId();
	}
}
